package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import java.awt.image.ImageObserver;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

@SuppressWarnings("serial")
public class TasksListingPanel extends JPanel {
	private String[] columnNames;
	private JScrollPane scrollPane;
	private JTable table;
	private static String[][] taskTable;
	private DefaultTableModel model;
	private UserCommand userCommand = UserCommand.getInstance();
	private int rowSelected = -1;

	public TasksListingPanel() {
		setEnvironment();

		columnNames = new String[] { "ID", "Start-Date", "Start-Time",
				"End-Date", "End-Time", "Task" };

		model = new DefaultTableModel();
		for (String column : columnNames) {
			model.addColumn(column);
		}

		table = new JTable(model) {
			public boolean isCellEditable(int row, int col) {
				return false;
			};

			{
				setOpaque(false);
			}
		};

		ImageIcon icon = new ImageIcon(ImagesDirectory.getTaskTableBG());
		final Image backgroundImage = icon.getImage();
		final ImageObserver imageObserver = icon.getImageObserver();

		scrollPane = new JScrollPane(table) {
			{
				setOpaque(false);
				getViewport().setOpaque(false);

			}

			@Override
			protected void paintComponent(Graphics g) {
				// Draw the background image.
				g.drawImage(backgroundImage, 0, 0, Dimensions._windowWidth,
						Dimensions._windowHeight, imageObserver);
				super.paintComponent(g);
			}
		};

		setScrollPaneProporties();
		setTableProperties();
		add(scrollPane, BorderLayout.CENTER);
		setTableEnvironment();
		table.repaint();

	}

	private void setScrollPaneProporties() {
		scrollPane.getVerticalScrollBar().setOpaque(false);
		scrollPane.getVerticalScrollBar().setUI(new CustomizedScrollUI());
		scrollPane.setColumnHeader(new JViewport());
		scrollPane.getColumnHeader().setOpaque(false);
	}

	private void setTableEnvironment() {
		setPreferredSize(new Dimension(Dimensions._tablePanelWidth,
				Dimensions._tablePanelHeight));
		setMinimumSize(new Dimension(Dimensions._tablePanelWidth,
				Dimensions._tablePanelHeight));
		setMaximumSize(new Dimension(Dimensions._tablePanelWidth,
				Dimensions._tablePanelHeight));
	}

	private void setTableProperties() {
		table.setRowSelectionAllowed(true);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.addFocusListener(new TableFocusListener());
		table.setSelectionForeground(Color.BLACK);
		table.setFillsViewportHeight(true);
		table.setShowGrid(false);
		table.setRowHeight(20);
		table.getTableHeader().setReorderingAllowed(false);
		table.getTableHeader().setOpaque(false);

		initTableColumns();
	}

	private void setEnvironment() {
		setLayout(new BorderLayout());
		setPreferredSize(new Dimension(Dimensions._windowWidth,
				Dimensions._windowHeight));
		setOpaque(false);
	}

	private void initTableColumns() {
		CustomTableCellRenderer cellRenderer = new CustomTableCellRenderer();
		CustomHeaderRenderer headerRenderer = new CustomHeaderRenderer();
		TableColumn tableColumn = table.getColumnModel().getColumn(0);
		tableColumn.setMinWidth(20);
		tableColumn.setMaxWidth(20);
		tableColumn.setHeaderRenderer(headerRenderer);
		tableColumn.setCellRenderer(cellRenderer);
		tableColumn = table.getColumnModel().getColumn(1);
		tableColumn.setMinWidth(100);
		tableColumn.setMaxWidth(100);
		tableColumn.setHeaderRenderer(headerRenderer);
		tableColumn.setCellRenderer(cellRenderer);
		tableColumn = table.getColumnModel().getColumn(2);
		tableColumn.setMinWidth(100);
		tableColumn.setMaxWidth(100);
		tableColumn.setHeaderRenderer(headerRenderer);
		tableColumn.setCellRenderer(cellRenderer);
		tableColumn = table.getColumnModel().getColumn(3);
		tableColumn.setMinWidth(100);
		tableColumn.setMaxWidth(100);
		tableColumn.setHeaderRenderer(headerRenderer);
		tableColumn.setCellRenderer(cellRenderer);
		tableColumn = table.getColumnModel().getColumn(4);
		tableColumn.setMinWidth(100);
		tableColumn.setMaxWidth(100);
		tableColumn.setHeaderRenderer(headerRenderer);
		tableColumn.setCellRenderer(cellRenderer);
		tableColumn = table.getColumnModel().getColumn(5);
		tableColumn.setHeaderRenderer(headerRenderer);
		tableColumn.setCellRenderer(cellRenderer);

		repaint();
	}

	private void loadTable(String[][] taskTable) {
		clearTable();
		for (int i = 0; i < taskTable.length; i++) {
			model.addRow(taskTable[i]);
		}
		table.validate();
		table.repaint();
	}

	private void clearTable() {
		int rows = model.getRowCount();
		for (int i = 0; i < rows; i++) {
			model.removeRow(0);
		}
	}

	protected void updateTable(String[][] tableContents) {
		taskTable = tableContents;
		loadTable(taskTable);
	}

	public class CustomHeaderRenderer extends JLabel implements
			TableCellRenderer {

		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			setFont(new java.awt.Font("Comic Sans MS", Font.BOLD, 15));
			setText((String) value);
			setHorizontalAlignment(SwingConstants.CENTER);
			setForeground(Color.CYAN);
			return this;
		}
	}

	public class CustomTableCellRenderer extends JLabel implements
			TableCellRenderer {

		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			setFont(new java.awt.Font("Comic Sans MS", Font.BOLD, 14));
			setText((String) value);
			setHorizontalAlignment(SwingConstants.CENTER);
			setOpaque(false);

			setForeground(new Color(0, 204, 204));
			rowSelected = table.getSelectedRow();
			if (row == rowSelected && table.hasFocus()) {
				setForeground(Color.BLACK);
				String temp = (String) table.getValueAt(row, 5);
				userCommand.currRowSelection("Task: " + temp);
			} else {
				setForeground(new Color(0, 204, 204));
			}
			return this;
		}
	}

	public class TableFocusListener implements FocusListener {

		@Override
		public void focusGained(FocusEvent arg0) {
			table.repaint();
		}

		@Override
		public void focusLost(FocusEvent arg0) {
			table.repaint();
			if (!userCommand.hasFocus())
				userCommand.currRowSelection(" ");
		}
	}

}